/* Copyright © Worklight Ltd. 2006-2011.  All rights reserved. */

package com.worklight.wlclient;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

public class WLConfig {
	
	
	private static final String WL_PREFS = "WLPrefs";
	public static final String WL_CLIENT_PROPS_NAME = "wlclient.properties";
	public static final String WL_DOUBLE_COOKIE_HEADER = "x-wl-app-version";
	
	public static final String ENABLE_SETTINGS = "enableSettings";
	private static final String WL_APP_VERSION = "wlAppVersion";
	private static final String WL_APP_ID = "wlAppId";
	private static final String WL_SERVER_HOST = "wlServerHost";
	private static final String WL_SERVER_PROTOCOL = "wlServerProtocol";
	private static final String WL_SERVER_PORT = "wlServerPort";
	private static final String WL_C2DM_SENDER = "wlC2DMEmailSender";
	private static final String WL_MAIN_FILE_PATH = "wlMainFilePath";	
	private Properties wlProperties = new Properties();
	private String wlServerFromSettings = null;
	private SharedPreferences prefs = null;

	public WLConfig(Context context){
		try {
			wlProperties.load(context.getAssets().open(WL_CLIENT_PROPS_NAME));
			prefs = context.getSharedPreferences(WL_PREFS, 0);
		} catch (IOException e) {
			throw new RuntimeException ("WLConfig(): Can't load " + WL_CLIENT_PROPS_NAME + " file");
		}
	}

	public WLConfig(Activity context){
		this(context.getApplication());
	}
	
	public URL getRootURL() {
		try {
			String port = (String) wlProperties.get(WL_SERVER_PORT);
			String rootUrl = null;	
			wlServerFromSettings = prefs.getString("WLServerURL", null);
			if (wlServerFromSettings != null) {
				rootUrl = String.format("%s/apps/services/api/%s/android/0/", wlServerFromSettings,	wlProperties.get(WL_APP_ID));
			} else if (port != null && !port.equals("-1")) {
				rootUrl = String.format("%s://%s:%s/apps/services/api/%s/android/0/", 
						wlProperties.get(WL_SERVER_PROTOCOL), 
						wlProperties.get(WL_SERVER_HOST), 
						port,
						wlProperties.get(WL_APP_ID));
			} else {
				rootUrl = String.format("%s://%s/apps/services/api/%s/android/0/",
						wlProperties.get(WL_SERVER_PROTOCOL),
						wlProperties.get(WL_SERVER_HOST),
						wlProperties.get(WL_APP_ID));
			}
			return new URL(rootUrl);
		} 
		catch (MalformedURLException e) {
			throw new RuntimeException("Could not parse URL; check assets/" + WL_CLIENT_PROPS_NAME + ". " + e.getMessage(), e);
		}
	}
	
	public URL getBaseServerURL() {
		try {
			String port = (String) wlProperties.get(WL_SERVER_PORT);
			return new URL(String.format("%s://%s:%s", wlProperties.get(WL_SERVER_PROTOCOL), wlProperties.get(WL_SERVER_HOST), port));
		} 
		catch (MalformedURLException e) {
			throw new RuntimeException("Could not parse URL; check assets/" + WL_CLIENT_PROPS_NAME + ". " + e.getMessage(), e);
		}
	}
		
	public String getApplicationVersion () {		
		return (String)wlProperties.get(WL_APP_VERSION);
	}	
	
	public String getC2DMSender () {
		String wlC2DMSender = wlProperties.getProperty(WL_C2DM_SENDER);
		if (wlC2DMSender != null) {
			return wlC2DMSender.trim();
		}
		return wlC2DMSender;
	}
	
	public String getMainFilePath () {		
		return (String)wlProperties.get(WL_MAIN_FILE_PATH);
	}
	
	public String getSettingsFlag () {		
		return (String)wlProperties.get(ENABLE_SETTINGS);
	}
}
